package com.yoo.ons.function.album.viewmodel

import androidx.lifecycle.MutableLiveData
import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.model.PersonAlbumModel
import com.yoo.ons.model.PictureManagerModel

class PersonAlbumViewModel:BaseVMViewModel() {

    val personAlbumLiveData = MutableLiveData<PersonAlbumModel>()
    /**
     * 查询用户个人相册
     */
    fun getPersonAlbum(){
        val listener = object : HttpRequestListener<PersonAlbumModel>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["type"]=3
            }

            override fun success(t: PersonAlbumModel?) {
            }
        }
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPersonAlbums(map)?.requestObserver(
                    NetworkObserver<PersonAlbumModel>().startRequest {
                    }.success {
                        val pictureManagerModel = PictureManagerModel(
                            "",
                            "", "", false
                        )
                        it?.images?.add(pictureManagerModel)//添加空model，以便展示添加图片按钮
                        personAlbumLiveData.value = it
                        //listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }
    fun getPersonAlbumAdd(listener:HttpRequestListener<PictureManagerModel>){
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPersonAlbumsAdd(map)?.requestObserver(
                    NetworkObserver<PictureManagerModel>().startRequest {
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }

    fun getPersonAlbumRemove(listener:HttpRequestListener<PictureManagerModel>){
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPersonAlbumsRemove(map)?.requestObserver(
                    NetworkObserver<PictureManagerModel>().startRequest {
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }

}